//
//  VideoMainController.m
//  FoodList
//
//  Created by Jason on 16/4/8.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "VideoMainController.h"
#import "VideoTableViewCell.h"
#import "VideoModel.h"
#import "VideoDetailController.h"

@interface VideoMainController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger startNumber;

@end

@implementation VideoMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.videoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.videoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestVideoData];
    }];
    
    self.videoTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreVideoData];
    }];
    
    
}

- (void)requestVideoData {
    [RequestManger requestWithURL:@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=%E5%BC%80%E8%83%83" requestType:nil parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *videoListArr = dataDic[@"videoList"];
        
        NSMutableArray *modelArr = [NSMutableArray array];
        for (NSDictionary *dic in videoListArr) {
            VideoModel *model = [[VideoModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            NSDictionary *consumptionDic = dic[@"consumption"];
            [model setValuesForKeysWithDictionary:consumptionDic];
            model.myDescription = dic[@"description"];
            [modelArr addObject:model];
        }
        self.modelArray = modelArr;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.videoTableView reloadData];
            [self.videoTableView.mj_header endRefreshing];
        });
        
    } error:^(NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)loadMoreVideoData {
    _startNumber += 10;
    NSString *hha = @"%E5%BC%80%E8%83%83";
    NSString *urlStr = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/videos?start=%ld&num=10&categoryName=%@&strategy=date&vc=181", _startNumber, hha];
    [RequestManger requestWithURL:urlStr requestType:nil parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *videoListArr = dataDic[@"videoList"];
        for (NSDictionary *dic in videoListArr) {
            VideoModel *model = [[VideoModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            NSDictionary *consumptionDic = dic[@"consumption"];
            [model setValuesForKeysWithDictionary:consumptionDic];
            model.myDescription = dic[@"description"];
            [self.modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.videoTableView reloadData];
            [self.videoTableView.mj_footer endRefreshing];
        });
        
    } error:^(NSError *error) {
        NSLog(@"fail");
    }];
}

#pragma mark - tableView 代理方法 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videoCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"VideoTableViewCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _modelArray[indexPath.row];
    return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return Height / 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoDetailController *detailVc = [[VideoDetailController alloc] initWithNibName:@"VideoDetailController" bundle:[NSBundle mainBundle]];
    detailVc.VideoModelArray = _modelArray;
    detailVc.number = indexPath.row;
    [self.navigationController pushViewController:detailVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
